package com.zh.work.controller.platform.branch;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.work.dao.platform.branch.BranchAppMapper;
import com.zh.work.entity.branch.BranchAppEntity;
import com.zh.work.entity.branch.BranchValid;
import com.zh.common.entity.JSONResult;
import com.zh.work.service.platform.branch.BranchAppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: app管理管理
 * @Author: Li.HuiNeng
 * @Create in: 2019/10/24
 * @Version: 1.0.1
 */
@Api(value = "app管理管理控制器", produces = "application/json", tags = {"app管理管理控制器"})
@RestController
@RequestMapping("/branchApp")
public class BranchAppController {

    /**
     * @Description: 简易crud时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/10/24
     */
    @Autowired
    private BranchAppMapper mapper;

    /**
     * @Description: 有业务逻辑需要处理时使用
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/10/24
     */
    @Autowired
    private BranchAppService service;

    /**
     * @Description: redis实例
     * ------------------------
     * @Author: Li.HuiNeng
     * @Date: 2019/10/24
     */
    @Autowired
    private RedisTemplate redisTemplate;

    @ApiOperation(value = "新增一条记录", tags = {"putBranchApp"}, notes = "新增一条记录")
    @Transactional
    @PutMapping("/putBranchApp")
    @ResponseBody
    public JSONResult<Object> putAddBranchApp(@ApiParam(value = "app管理实体类", name = "BranchApp") @RequestBody BranchAppEntity branchAppEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (branchAppEntity.insert()) {
                // 插入成功则插入银行卡鉴权资料
                List<BranchValid> list = JSON.parseArray(branchAppEntity.getJsonValid(), BranchValid.class);
                if (list != null) {
                    if (list.size() > 0) {
                        list.forEach(l -> l.setBranchNo(branchAppEntity.getBranchNo()));
                        this.service.addBranchValid(list);
                    }
                }
                jsonResult.setCode(200);
                jsonResult.setMessage("添加完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            e.printStackTrace();
        }
        return jsonResult;
    }


    @ApiOperation(value = "通过id修改信息", tags = {"putModBranchAppById"}, notes = "通过id修改信息")
    @PutMapping("/putModBranchAppById")
    @ResponseBody
    public JSONResult<Object> putModBranchAppById(@ApiParam(value = "app管理实体类", name = "branchApp") @RequestBody BranchAppEntity branchAppEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            // 分解二维码成本信息
            List<BranchValid> list = JSON.parseArray(branchAppEntity.getJsonValid(), BranchValid.class);
            assert (StringUtils.isNotEmpty(branchAppEntity.getBranchNo()));
            Map<String, Long> map = mapper.getBranchAppNum(branchAppEntity.getBranchNo());
            if (map != null) {
                if (map.get("num") != 0) {
                    // 批量删除
                    int i = this.service.delBranchValid(branchAppEntity.getBranchNo());
                }
            }
            if (list != null) {
                if (list.size() > 0) {
                    list.forEach(l -> l.setBranchNo(branchAppEntity.getBranchNo()));
                    // 批量插入当前机构的二维码成本信息
                    this.service.addBranchValid(list);
                }
            }
            if (branchAppEntity.updateById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("修改完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("修改失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setCode(e.hashCode());
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键删除记录", tags = {"deleteBranchAppById"}, notes = "根据主键删除记录")
    @DeleteMapping("/deleteBranchAppById")
    public JSONResult<Object> deleteBranchAppById(@ApiParam(value = "app管理实体类", name = "BranchApp") BranchAppEntity BranchAppEntity) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            if (BranchAppEntity.deleteById()) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据主键列表批量删除记录", tags = {"deleteBranchAppBatchByIds"}, notes = "该传参时 请选择raw(application/json)格式传入")
    @DeleteMapping("/deleteBranchAppBatchByIds")
    @ResponseBody
    public JSONResult<Object> deleteBranchAppBatchByIds(@ApiParam(value = "主键集合", name = "ids") @RequestBody @RequestParam("ids") List<Integer> ids) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
            int c = this.mapper.deleteBatchIds(ids);
            if (c > 0) {
                jsonResult.setCode(200);
                jsonResult.setMessage("删除完成");
                jsonResult.setSuccess(true);
            } else {
                jsonResult.setMessage("删除失败,请检查参数是否填写完整!");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }

   /* @ApiOperation(value = "根据id查询详细信息", tags = {"getBranchAppById"}, notes = "根据id查询详细信息")
    @GetMapping("/getBranchAppById")
    public JSONResult<BranchApp> getBranchAppById(@ApiParam(value = "app管理实体类", name = "BranchApp") BranchApp BranchApp) {
        JSONResult<BranchApp> jsonResult = new JSONResult<>();
        try {
            BranchApp result = (BranchApp) BranchApp.selectById(BranchApp.getId());
            if (result != null) {
                jsonResult.setCode(200);
                jsonResult.setMessage("查询完成");
                jsonResult.setSuccess(true);
                jsonResult.setData(result);
            } else {
                jsonResult.setMessage("未查询到相应信息，请检查参数是否正确");
                jsonResult.setSuccess(false);
            }
        } catch (Exception e) {
            jsonResult.setSuccess(false);
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setCode(e.hashCode());
            e.printStackTrace();
        }
        return jsonResult;
    }*/


    @ApiOperation(value = "分页查询参数数据集", tags = {"getPagesBranchApps"}, notes = "分页查询参数数据集")
    @GetMapping("/getPagesBranchApps")
    public JSONResult<List<BranchAppEntity>> getPagesBranchApps(
            @ApiParam(value = "起始页", name = "pageNo") Integer pageNo,
            @ApiParam(value = "当前页显示数量", name = "pageNo") Integer pageSize,
            @ApiParam(value = "app管理实体类", name = "branchApp") BranchAppEntity branchAppEntity) {
        JSONResult<List<BranchAppEntity>> jsonResult = new JSONResult<>();
        try {
            IPage<BranchAppEntity> page = new Page<>(pageNo, pageSize);
            QueryWrapper<BranchAppEntity> wrapper = new QueryWrapper<>();
            wrapper.setEntity(branchAppEntity);
            IPage<BranchAppEntity> result = this.mapper.selectPage(page, wrapper);
            jsonResult.setTotal(page.getTotal());
            jsonResult.setData(result.getRecords());

//            redisTemplate.opsForValue().set("test", list);
//            List<ParamEntity> list1 = (List<ParamEntity>)redisTemplate.opsForValue().get("test");
            jsonResult.setMessage("操作成功");
            jsonResult.setSuccess(true);
            jsonResult.setCode(200);
        } catch (Exception e) {
            jsonResult.setMessage(e.getLocalizedMessage());
            jsonResult.setSuccess(false);
            e.printStackTrace();
        }
        return jsonResult;
    }

    @ApiOperation(value = "用于获取指定机构的鉴权资料", tags = {"getBranchValid"}, notes = "用于获取指定机构的鉴权资料")
    @GetMapping("/getBranchValid")
    public JSONResult<List<BranchValid>> getBranchValid(@ApiParam(value = "鉴权资料实体信息", name = "branchValid")BranchValid branchValid) {
        JSONResult<List<BranchValid>> jsonResult = new JSONResult<>();
        List<BranchValid> list = this.service.getBranchValid(branchValid);
        if (list.size() > 0) {
            jsonResult.setData(list);
            jsonResult.setSuccess(true);
        } else {
            jsonResult.setSuccess(false);
            jsonResult.setMessage("未找到编号:" + branchValid.getBranchNo() + "的机构信息.");
        }
        return jsonResult;
    }

}
